#include "fight.h"
#include "ui_fight.h"

Fight::Fight(QWidget *parent, int id) :
    QWidget(parent),
    ui(new Ui::Fight)
{
    ui->setupUi(this);
    this->id = id;
    clickRight = true;

    // 建立数据库连接
    dbase = new DbOperator();
    dbase->OpenDb();

    // 加载玩家数据与参与战斗的宝可梦数据
    trainer = dbase->searchTrainerMsg(id);
    QStringList l = trainer.idno.split(".");
    QString i = l.at(0);
    int idno = i.toInt();
    fight_pet = dbase->searchPokemonMsg(idno);

    // 设置战斗时boss的数据
    bossMaxHp = 50;
    bossCurHp = 50;
    bossAttack = 17;
    bossDefense = 33;
    boss_state[0] = QString("【喵喵】 使用了 嚎叫\n似乎没有起到效果\n造成了%1的伤害\n");
    boss_state[1] = QString("【喵喵】 使用了 疯狂乱抓\n效果拔群！！\n造成了%1的伤害\n");
    boss_state[2] = QString("【喵喵】 使用了 噪音\n造成了%1的伤害\n");
    boss_state[3] = QString("【喵喵】 使用了 劈开\n暴击了！！\n造成了%1的伤害\n");
    boss_skill_num[0] = 0;
    boss_skill_num[1] = 20;
    boss_skill_num[2] = 10;
    boss_skill_num[3] = 20;
    order = 0;       // 事件触发次序
    ui->boss_cur_hp->setText(QString("怪物当前血量为：%1").arg(bossCurHp));


    // 设置参战pokemon的战斗数据：
    curhp = fight_pet.curhp;
    QString path = QString(":/bar/images/head (%1).png").arg(fight_pet.id);
    QImage* img = new QImage(path);
    show_pic(img, ui->icon);
    ui->cur_hp->setText(QString("当前血量为：%1").arg(curhp));

    //加载技能
    show_skill();

    ui->fight_msg->setText(QString("战斗开始！\n"));

}

Fight::~Fight()
{
    delete ui;
    if(dbase != 0){
        delete dbase;
        dbase = 0;
    }
}

void Fight::show_skill()
{
    pokemon temp = fight_pet;
    skill = temp.skill.split(".");
    QString skillnum;
    QImage* img;
    QString picpath;
    // 设置技能文字和技能图标
    skillnum = skill.at(0);
    ui->skill_1->setText(con.getSkillDgb(temp.id, skillnum.toInt()));
    picpath = con.getSkillIcon(con.getSkillAttr(temp.id, skillnum.toInt()));
    img = new QImage(picpath);
    show_pic(img, ui->icon_1);

    skillnum = skill.at(1);
    ui->skill_2->setText(con.getSkillDgb(temp.id, skillnum.toInt()));
    picpath = con.getSkillIcon(con.getSkillAttr(temp.id, skillnum.toInt()));
    img = new QImage(picpath);
    show_pic(img, ui->icon_2);

    skillnum = skill.at(2);
    ui->skill_3->setText(con.getSkillDgb(temp.id, skillnum.toInt()));
    picpath = con.getSkillIcon(con.getSkillAttr(temp.id, skillnum.toInt()));
    img = new QImage(picpath);
    show_pic(img, ui->icon_3);

    skillnum = skill.at(3);
    ui->skill_4->setText(con.getSkillDgb(temp.id, skillnum.toInt()));
    picpath = con.getSkillIcon(con.getSkillAttr(temp.id, skillnum.toInt()));
    img = new QImage(picpath);
    show_pic(img, ui->icon_4);
}

void Fight::skill_calculate_and_show(int no)        // 伤害结算和展示
{
    if (clickRight == true) {
        clickRight = false;
        QTextCharFormat fmt;
        QColor color;
        QRgb mr = qRgb(255, 203, 203);
        color.setRgb(mr);
        fmt.setForeground(color);
        ui->fight_msg->setCurrentCharFormat(fmt);

        if(order == 4) {
            order = 0;
        }
        // 分析信息
        QString skillnum = skill.at(no-1);
        int skn = skillnum.toInt();
        QString name = con.getSkillName(fight_pet.id, skn);
        // 拼接打印信息
        QString temp = QString("【%1】 使用了 %2\n").arg(con.getName(fight_pet.id)).arg(name);
        int init_num = con.getSkillhurt(fight_pet.id, skn);
        int damage = con.get_hurt_num(init_num, fight_pet.attack, bossDefense);
        temp = temp + QString("造成了%1点伤害\n").arg(damage);
        // 打印信息
        ui->fight_msg->append(temp);
        bossCurHp = bossCurHp - damage;
        ui->fight_msg->moveCursor(QTextCursor::End);       // 光标沉底
        if (bossCurHp <= 0) {
            // 更新血量显示
            ui->boss_cur_hp->setText(QString("怪物当前血量：%1").arg(0));
            ui->fight_msg->append("给出了致命一击！");
            success();
        } else {
            ui->boss_cur_hp->setText(QString("怪物当前血量：%1").arg(bossCurHp));
            // 1s
            Sleep(1500);
            // boss伤害计算显示
            mr = qRgb(255, 120, 102);
            color.setRgb(mr);
            fmt.setForeground(color);
            ui->fight_msg->setCurrentCharFormat(fmt);

            damage = con.get_hurt_num(boss_skill_num[order], bossAttack, fight_pet.defense);
            temp = boss_state[order].arg(damage);
            ui->fight_msg->append(temp);
            ui->fight_msg->moveCursor(QTextCursor::End);
            curhp = curhp-damage;
            order++;
            if (curhp > 0) {
                ui->cur_hp->setText(QString("当前血量：%1").arg(curhp));
            } else {
                ui->cur_hp->setText(QString("当前血量：%1").arg(0));
                ui->fight_msg->append("给出了致命一击！");
                defeat();
            }
        }
        clickRight = true;    // 重新开启
    }
}

template<typename T>
void Fight::show_pic(QImage *img, T tool)
{
    img->scaled(tool->size(), Qt::KeepAspectRatio);    // 第二个参数中有三种模式，详情见https://blog.csdn.net/qq_21449473/article/details/103820668
    tool->setScaledContents(true);   // 是否允许缩放填充
    tool->setPixmap(QPixmap::fromImage(*img));
}


void Fight::on_skill_1_clicked()
{
    skill_calculate_and_show(1);
}

void Fight::on_skill_2_clicked()
{
    skill_calculate_and_show(2);

}

void Fight::on_skill_4_clicked()
{
    skill_calculate_and_show(4);

}

void Fight::on_skill_3_clicked()
{
    skill_calculate_and_show(3);

}

void Fight::Sleep(int msec)
{
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime ){
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    }
}

void Fight::success()
{
    QMessageBox msgbox;
    msgbox.setText("挑战成功   ");
    msgbox.setParent(this);
    msgbox.move(750, 400);
    msgbox.exec();

    msgbox.setText("金币+2，好感度+1");
    msgbox.exec();
    dbase->setcurHp(fight_pet.idno, curhp);
    dbase->updateGoldCoin(id, trainer.goldcoin+2);
    dbase->updatefetter(fight_pet.idno, fight_pet.fetter+1);
    this->close();
}

void Fight::defeat()
{
    QMessageBox msgbox;
    msgbox.setText("挑战失败, 请再接再厉");
    msgbox.setParent(this);
    msgbox.move(750, 400);
    msgbox.exec();
    dbase->setcurHp(fight_pet.idno, 0);
    this->close();
}

void Fight::closeEvent(QCloseEvent *)
{
    emit clo();
    delete dbase;
    dbase = 0;
}
